package com.file.system.tools.console;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.file.system.tools.xml.Pather;

import java.io.*;

/**
 * Created by jch on 2019/12/17.
 */
public class Read {
    //颜色
    private static String[][] colors = {
            {"\033[30;1m", "\033[0m"},
            {"\033[31;1m", "\033[0m"},
            {"\033[32;1m", "\033[0m"},
            {"\033[33;1m", "\033[0m"},
            {"\033[34;1m", "\033[0m"},
            {"\033[35;1m", "\033[0m"},
            {"\033[36;1m", "\033[0m"},
            {"\033[37;1m", "\033[0m"},
            {"\033[38;1m", "\033[0m"}
    };
    //背景颜色
    private static String[][] backgroudColors = {
            {"\033[40;31;3m", "\033[0m"},
            {"\033[41;32;3m", "\033[0m"},
            {"\033[42;33;3m", "\033[0m"},
            {"\033[43;34;3m", "\033[0m"},
            {"\033[44;35;3m", "\033[0m"},
            {"\033[45;36;3m", "\033[0m"},
            {"\033[46;37;3m", "\033[0m"},
            {"\033[47;31;3m", "\033[0m"}
    };
    //颜色顺序
    private static int num = 0;
    //背景颜色顺序
    private static int backgroudNum = 0;

    private static String[] strs = {
            "+---------------------------------------------------------------------------------------------------------------------------+",
            "|                                                                                                                           |",
            "|                                                                                                                           |",
            "+---------------------------------------------------------------------------------------------------------------------------+"
    };

    public static void console(String s) {
        print(s, "String");
    }

    public static void console(int num) {
        print(String.valueOf(num), "int");
    }

    public static void console(char c) {
        print(String.valueOf(c), "char");
    }

    public static void console(double d) {
        print(String.valueOf(d), "double");
    }

    public static void console(long l) {
        print(String.valueOf(l), "long");
    }

    public static void console(float f) {
        print(String.valueOf(f), "float");
    }

    public static void console(boolean b) {
        print(String.valueOf(b), "boolean");
    }


    public static void main(String[] args) {
        /*System.out.println(AnsiColor.BRIGHT_GREEN.toString());
        console("995年贸易谈崩，冈比亚：“以远征军横扫欧洲大陆”");
        console(-1);
        console(11.234);
        console(11L);
        console(11.33445f);
        console(false);
        console(null);*/
        console("start");
//        System.out.println(strLength("这是一个测试sds"));
    }

    /**
     * 获取颜色
     */
    private static String selectAsciiColor(String str) {
        str = colors[num][0] + str + colors[num][1];
        //获取下一颜色顺序
        num++;
        //当颜色值大于颜色总数时  重置颜色顺序
        if (num >= colors.length) {
            num = 0;
        }
        return str;
    }

    /**
     * 获取背景颜色
     */
    private static String selectAsciiBackgroudColor(String str) {
        str = backgroudColors[backgroudNum][0] + str + backgroudColors[backgroudNum][1];
        //获取下一颜色顺序
        backgroudNum++;
        //当颜色值大于颜色总数时  重置颜色顺序
        if (backgroudNum >= backgroudColors.length) {
            backgroudNum = 0;
        }
        return str;
    }

    /**
     * 判断文件是否存在
     *
     * @date
     */
    private static boolean checkFileIsExit(String fileUrl) {
        if (StrUtil.isEmpty(fileUrl)) {
            return false;
        }
        File file = new File(fileUrl);
        if (!file.exists()) {
            return false;
        }
        return true;
    }


    /**
     * 打印文本
     *
     * @date
     */
    private static void print(String str, String type) {
        //上传路径
        String fileUrl = Pather.getClassPath().split("WEB-INF")[0] + "WEB-INF/classes/console/" + str + ".txt";
        //判断文件是否存在
        if (!checkFileIsExit(fileUrl)) {
            printStr(str, type);
        } else {
            printTxt(fileUrl);
        }
    }

    /**
     * 打印txt文件
     *
     * @date
     */
    private static void printTxt(String fileUrl) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileUrl), "UTF-8"))) {
            String tempString;
            while ((tempString = reader.readLine()) != null) {
                System.out.println(selectAsciiColor(tempString));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 打印字符串
     *
     * @date
     */
    private static void printStr(String str, String type) {

        //计算居中所需追加的空格数
        int num = strs[0].length() - 2;
        if (ObjectUtil.isNull(str)) {
            str = "null";
        }
        str += "  -->(" + type + ")";
        str = "|" + appendSpace((num - strLength(str)) / 2) + str;
        str += appendSpace(num - strLength(str) + 1) + "|";

        System.out.println(selectAsciiColor(strs[0]));
        System.out.println(selectAsciiColor(strs[1]));
        System.out.println(selectAsciiColor(str));
        System.out.println(selectAsciiColor(strs[2]));
        System.out.println(selectAsciiColor(strs[3]));
    }

    /**
     * 获取长度
     *
     * @date
     */
    private static int strLength(String str) {
        int length = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) {
                length += 2;
            } else {
                length++;
            }
        }
        return length;
    }


    /**
     * 判断是否为汉字
     *
     * @date
     */
    private static boolean isChineseByScript(char c) {
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }

    /**
     * 判断是否有中文标点
     *
     * @date
     */
    private static boolean isChinesePunctuation(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
                || ub == Character.UnicodeBlock.VERTICAL_FORMS) {
            return true;
        } else {
            return false;
        }
    }

    private static String appendSpace(int num) {
        StringBuilder space = new StringBuilder();
        for (int i = 0; i < num; i++) {
            space.append(" ");
        }
        return space.toString();
    }

}
